<?php

//会员
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');

class edit extends foreground {

    public function __construct() {
        parent::__construct();

        $this->db_member = pc_base::load_model('member_model');
    }

    //头像
    public function thumb() {
        $uid = (int)param::get_cookie('_userid');

        if (!isset($_POST['dosubmit'])) {
            $info = $this->memberinfo;
            include template('member', 'edit_thumb');
            exit();
        }

        pc_base::load_sys_class('attachment', '', 0);
        $attachment = new attachment();

        $r = $attachment->upload('userthumb');
        if ($r) {
            $thumb = APP_PATH . 'uploadfile/' . $attachment->uploadedfiles[0]['filepath'];
            $this->db_member->update(['thumb' => $thumb], ['userid' => $uid]);

            success_m('头像修改成功', MEMBER_PATH . '&c=index&a=info');
        } else {
            error_m('上传失败', 'goback');
        }
    }

    //ajax
    public function part() {
        $uid = (int)param::get_cookie('_userid');

        if (!isset($_POST['name']) || !isset($_POST['val']) || $_POST['name'] == '' || $_POST['val'] == '') {
            json_exit(1, 300, '请输入要填写的信息');
        }
        $name = $_POST['name'];
        $val = $_POST['val'];

        if (in_array($name, ['realname', 'field', 'job', 'company'])) {
            $val = safe_replace($val);
        } elseif ($name == 'idcard') {
            if (!preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/i", $val)) {
                json_exit(1, 300, '身份证输入有误');
            }
        } elseif ($name == 'birthday') {
            if (!preg_match("/^(\d{4}-\d{2}-\d{2})$/", $val)) {
                json_exit(1, 300, '生日输入有误');
            }
        } elseif ($name == 'sex') {
            if (!in_array($val, [1, 2])) {
                json_exit(1, 300, '性别选择有误');
            }
        } elseif ($name == 'mobile') {
            if (!preg_match("/^1\d{10}$/", $val)) {
                json_exit(1, 300, '手机号码输入有误');
            }
            
        } else {
            json_exit(1, 300, '该选项暂时不支持修改');
        }

        $r = $this->db_member->update([$name => $val], ['userid' => $uid]);

        $one = $this->db_member->get_one(['userid' => $uid]);

        if ($one['full'] != 1) {
            $is_all = 1;
            foreach (['realname', 'sex', 'birthday', 'field', 'job', 'company'] as $v) {//, 'idcard'
                if (!$one[$v]) {
                    $is_all = 0;
                    break;
                }
            }
            if ($is_all) {
                $r = $this->db_member->update(['full' => 1], ['userid' => $uid]);

                //加 200 积分
                $db_member = pc_base::load_model('member_model');
                $cha = $db_member::log($uid, 200, $db_member::TYPE_FULL_INFO, '完善全部资料 +200', __METHOD__);

                json_exit('all', 100, '完善注册信息');
            }
        }

        json_exit(1, 100, '修改成功');
    }

}